﻿////////////////////////////////////////////////////////////////////////////////////////////
// File: Keyboard.cs                                                                      //
// Author: Mike Binnix                                                                    //
// Desc: Class representing a computer keyboard.  Wraps the XNA input methods for         //
//             keyboards into a single class to more easily check a users input.          //
////////////////////////////////////////////////////////////////////////////////////////////

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace SlothEngine.Input
{
    public class XNAKeyboard : IInputDevice
    {
        private KeyboardState currentState;
        private KeyboardState previousState;
        private bool initialized;
        private PlayerID player;

        public bool Initialized
        {
            get
            {
                return initialized;
            }
        }
        public PlayerID Player
        {
            get
            {
                return player;
            }
            set
            {
                if (value == PlayerID.All)
                    throw new Exception("Can not assign PlayerID to PlayerID." + value.ToString());
                else
                    player = value;
            }
        }

        public XNAKeyboard()
        {
            currentState = new KeyboardState();
            previousState = new KeyboardState();
            player = PlayerID.One;

            initialized = true;
        }
        public XNAKeyboard(PlayerID player)
        {
            currentState = new KeyboardState();
            previousState = new KeyboardState();

            if (player == PlayerID.All)
                throw new Exception("Can not assign PlayerID to PlayerID." + player.ToString());
            else
                this.player = player;

            initialized = true;
        }

        /// <summary>
        /// Updates the state of all keyboard keys each frame.
        /// </summary>
        /// <param name="gameTime">Games time.</param>
        public void Update(GameTime gameTime)
        {
            previousState = currentState;
            currentState = Microsoft.Xna.Framework.Input.Keyboard.GetState();
        }
        /// <summary>
        /// Checks if a keyboard button has been pressed in the frame frame.
        /// </summary>
        /// <param name="key">Key to check against.</param>
        /// <returns>True if key was pressed on last frame.  Otherwise false.</returns>
        public bool KeyPressed(Keys key)
        {
            if ((currentState.IsKeyDown(key)) && (!previousState.IsKeyDown(key)))
                return true;
            else
                return false;
        }
        /// <summary>
        /// Checks if a key was released during the last frame.
        /// </summary>
        /// <param name="key">Key to check against</param>
        /// <returns>True if button was released.  Otherwise false.</returns>
        public bool KeyReleased(Keys key)
        {
            if ((currentState.IsKeyUp(key)) && (previousState.IsKeyDown(key)))
                return true;
            else
                return false;
        }
        /// <summary>
        /// Checks if a key was held for multiple frames.
        /// </summary>
        /// <param name="key">Key to check against.</param>
        /// <returns>True if button was held.  Otherwise false.</returns>
        public bool KeyHeld(Keys key)
        {
            if ((currentState.IsKeyDown(key)) && (previousState.IsKeyDown(key)))
                return true;
            else
                return false;
        }
    }
}
